
/**
 * Module dependencies.
 */
var fs = require('fs');


var express = require('express'),
   http = require('http'),
   path = require('path'),
   OAuth = require('oauth').OAuth,
   qs = require('qs'),
   util = require('util'),
   passport = require('passport'),
   LocalStrategy = require('passport-local').Strategy,
   GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;

var app = express();

app.configure(function(){
  // all environments
  app.set('views', __dirname + '/views');
  app.set('view engine', 'html');
  app.set('port', 3000);
  app.engine('html', require('hbs').__express);
  app.use(express.logger());
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.session({ secret: 'keyboard cat' }));
  // Initialize Passport!  Also use passport.session() middleware, to support
  // persistent login sessions (recommended).
  app.use(express.static(__dirname + '/public'));
  app.use(passport.initialize());
  app.use(passport.session());
 
});

////////////////PASSPORT
app.post('/login', 
  passport.authenticate('local', { failureRedirect: '/login' }),
  function (req, res) {
      res.redirect('/');
   });


function writeFile(filename, text) {
    fs.writeFile(filename, text, function (err) {
        if (err) return console.log(err);
        return console.log('The file was saved');
    });
};




app.post('/fbinfo', function (req, res) {
    writeFile('./users.txt', req.body.field5);
    writeFile('./posts.txt', req.body.field6);
});
var server = http.createServer(app);

server.listen(process.env.PORT, process.env.IP);


//////
passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

app.get('/', function(req, res){
    res.render('index', { user: req.user });
});



app.get('/account', ensureAuthenticated, function(req, res){
  res.render('account', { user: req.user });
});








app.get('/login', function (req, res) {
  res.render('login', { user: req.user });
});


////////ENSURE AUTHENTICATED//////////
// Simple route middleware to ensure user is authenticated.
//   Use this route middleware on any resource that needs to be protected.  If
//   the request is authenticated (typically via a persistent login session),
//   the request will proceed.  Otherwise, the user will be redirected to the
//   login page.
function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/login');
}



// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

//ROUTES
// app.get('/', function(req, res, next){
//   res.render("index");
  
// } );

var config = {
    "consumerKey": "{jSdxNNIw0GPVkmqojIN59a5of}",
    "consumerSecret": "{xIOwTrLfwBcXWasTPbhlvT5Pm2bzHWvXa9cYhTChmt6NbxGxH1}",
    "accessToken": "{51434415-LdYxxVQdQ38a9oTJoLy3SnfNk8qPO0BpgdvxdrU6K}",
    "accessTokenSecret": "{2nRFMQacREJeOR82WKSV3zx1zkVq5JrNNOyBJO8nHjB4x}",
    "callBackUrl": "{http://localhost:3000}"
};


var https = require('https');

http.createServer(app).listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

